﻿using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace MvcMedicalProvider.Helpers
{
    public static class DBSafe
    {
        internal static int GetIntFromDB(IDataReader dr, string field)
        {
            return (!dr.IsDBNull(dr.GetOrdinal(field)) && !(dr[field].ToString().Trim().ToLower() == "null")) ? int.Parse(dr[field].ToString().Trim()) : -1;
        }

        internal static decimal GetDecimalFromDB(IDataReader dr, string field)
        {
            return !dr.IsDBNull(dr.GetOrdinal(field)) ? (decimal)dr[field] : 0;
        }

        internal static List<SqlDataRecord> GetUtblZipCodeDataRecords(List<string> dict)
        {
            if (dict == null || dict.Count < 1)
            {
                return null;
            }

            var dataRecords = new List<SqlDataRecord>();
            var sqlMetaDatas = new SqlMetaData[1];

            sqlMetaDatas[0] = new SqlMetaData("ZipCode", SqlDbType.VarChar, 5);

            //Binding rows
            foreach (var row in dict)
            {
                var dataRecord = new SqlDataRecord(sqlMetaDatas);
                dataRecord.SetString(0, row);

                dataRecords.Add(dataRecord);
            }

            return dataRecords;
        }

        internal static string RemoveSpecialCharacter(string input)
        {
            return input.Trim().Replace("'", "''").Replace("=", "").Replace("--", "");
        }
    }
}